# Maintainer: Sergey Popov <popov.sa@gmail.com>
# Contributor: Mario Emmenlauer <memmenlauer@biodataanalysis.de>

_realname=vtk
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=9.2.4
pkgrel=2
pkgdesc="A software system for 3D computer graphics, image processing and visualization (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://www.vtk.org/"
license=("BSD")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-double-conversion"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-fmt"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-tbb"
         "${MINGW_PACKAGE_PREFIX}-jsoncpp"
         "${MINGW_PACKAGE_PREFIX}-libharu"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-openxr-sdk"
         "${MINGW_PACKAGE_PREFIX}-pugixml"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-adios2"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-cgns"
             "${MINGW_PACKAGE_PREFIX}-cli11"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             "${MINGW_PACKAGE_PREFIX}-ffmpeg"
             "${MINGW_PACKAGE_PREFIX}-fontconfig"
             "${MINGW_PACKAGE_PREFIX}-freetype"
             "${MINGW_PACKAGE_PREFIX}-gdal"
             "${MINGW_PACKAGE_PREFIX}-gl2ps"
             "${MINGW_PACKAGE_PREFIX}-hdf5"
             "${MINGW_PACKAGE_PREFIX}-libjpeg"
             "${MINGW_PACKAGE_PREFIX}-liblas"
             "${MINGW_PACKAGE_PREFIX}-libmariadbclient"
             "${MINGW_PACKAGE_PREFIX}-libpng"
             "${MINGW_PACKAGE_PREFIX}-libogg"
             "${MINGW_PACKAGE_PREFIX}-libtheora"
             "${MINGW_PACKAGE_PREFIX}-libtiff"
             "${MINGW_PACKAGE_PREFIX}-libxml2"
             "${MINGW_PACKAGE_PREFIX}-netcdf"
             "${MINGW_PACKAGE_PREFIX}-openslide"
             "${MINGW_PACKAGE_PREFIX}-openturns"
             "${MINGW_PACKAGE_PREFIX}-openvdb"
             "${MINGW_PACKAGE_PREFIX}-openvr"
             "${MINGW_PACKAGE_PREFIX}-pdal"
             # "${MINGW_PACKAGE_PREFIX}-pegtl" https://gitlab.kitware.com/vtk/vtk/-/issues/18151
             "${MINGW_PACKAGE_PREFIX}-postgresql"
             "${MINGW_PACKAGE_PREFIX}-proj"
             "${MINGW_PACKAGE_PREFIX}-python"
             $([[ ${CARCH} != i686 ]] && echo \
               "${MINGW_PACKAGE_PREFIX}-qt6-declarative" || echo \
               "${MINGW_PACKAGE_PREFIX}-qt5-declarative")
             "${MINGW_PACKAGE_PREFIX}-sqlite3"
             # "${MINGW_PACKAGE_PREFIX}-tk"
             "${MINGW_PACKAGE_PREFIX}-unixodbc"
             "${MINGW_PACKAGE_PREFIX}-utf8cpp"
             )
optdepends=("${MINGW_PACKAGE_PREFIX}-adios2: I/O ADIOS2"
            "${MINGW_PACKAGE_PREFIX}-boost: InfovisBoost and InfovisBoostGraphAlgorithms modules"
            "${MINGW_PACKAGE_PREFIX}-cgns: CGNS Reader"
            "${MINGW_PACKAGE_PREFIX}-ffmpeg: I/O FFMPEG"
            "${MINGW_PACKAGE_PREFIX}-fontconfig: Freetype/Fontconfig Rendering"
            "${MINGW_PACKAGE_PREFIX}-freetype: Freetype Rendering"
            "${MINGW_PACKAGE_PREFIX}-gdal: I/O GDAL"
            "${MINGW_PACKAGE_PREFIX}-gl2ps: PS Rendering"
            "${MINGW_PACKAGE_PREFIX}-hdf5: I/O HDF5"
            "${MINGW_PACKAGE_PREFIX}-libjpeg: I/O Images"
            "${MINGW_PACKAGE_PREFIX}-liblas"
            "${MINGW_PACKAGE_PREFIX}-libmariadbclient: interface to MySQL/MariaDB"
            "${MINGW_PACKAGE_PREFIX}-libpng: I/O Images"
            "${MINGW_PACKAGE_PREFIX}-libogg: I/O OGG and Theora"
            "${MINGW_PACKAGE_PREFIX}-libtheora: I/O OGG and Theora"
            "${MINGW_PACKAGE_PREFIX}-libtiff: I/O Images"
            "${MINGW_PACKAGE_PREFIX}-libxml2: Infovis"
            "${MINGW_PACKAGE_PREFIX}-netcdf: I/O netCDF"
            "${MINGW_PACKAGE_PREFIX}-openslide: DomainsMicroscopy"
            "${MINGW_PACKAGE_PREFIX}-openturns"
            "${MINGW_PACKAGE_PREFIX}-openvdb: I/O OpenVDB"
            "${MINGW_PACKAGE_PREFIX}-openvr: OpenVR Rendering"
            "${MINGW_PACKAGE_PREFIX}-pdal: I/O PDAL"
            "${MINGW_PACKAGE_PREFIX}-postgresql: interface to PostgreSQL"
            "${MINGW_PACKAGE_PREFIX}-proj: GEOVis"
            "${MINGW_PACKAGE_PREFIX}-python: Python bindings"
            "${MINGW_PACKAGE_PREFIX}-python-matplotlib: Matplotlib renderer"
            "${MINGW_PACKAGE_PREFIX}-sqlite3: Interface to SQL"
            # "${MINGW_PACKAGE_PREFIX}-tk: TCL bindings, Python Tk widgets"
            "${MINGW_PACKAGE_PREFIX}-unixodbc")
if [[ ${CARCH} != i686 ]]; then
  optdepends+=("${MINGW_PACKAGE_PREFIX}-qt6-base: Qt Rendering"
               "${MINGW_PACKAGE_PREFIX}-qt6-declarative: Qt Qml plugin")
else
  optdepends+=("${MINGW_PACKAGE_PREFIX}-qt5-base: Qt Rendering"
               "${MINGW_PACKAGE_PREFIX}-qt5-declarative: Qt Qml plugin")
fi
source=(https://www.vtk.org/files/release/${pkgver%.*}/VTK-${pkgver}.tar.gz
        "006-link-to-pdalcpp.patch"
        "007-dll-export-some-functions.patch")
sha256sums=('9ed0d55c923778469357412d3f6bdd4841a31c30be997a53a7b0632d815cbc9c'
            'af98e72a0d64af3fa755929e724e900faa9ab466917fc24084026512f02399e4'
            'a9b9292be90c259f0aa058e3f4a7a08b6218f20e6cb2a71e214acda2d2302a50')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  apply_patch_with_msg \
    006-link-to-pdalcpp.patch

  # Succeed with Clang but failed with GCC!
  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    apply_patch_with_msg \
      007-dll-export-some-functions.patch
  fi
}

build() {
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CFLAGS+=" ${CPPFLAGS}"
  CXXFLAGS+=" ${CPPFLAGS} -Wno-attributes -Wno-ignored-attributes -Wno-deprecated-declarations"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -Wno-dev \
    -G"Ninja" \
    ${extra_config[@]} \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_INSTALL_LICENSEDIR=share/licenses/vtk \
    -DBUILD_SHARED_LIBS=ON \
    -DPython3_EXECUTABLE="${MINGW_PREFIX}/bin/python.exe" \
    -DVTK_BUILD_TESTING=OFF \
    -DVTK_VERSIONED_INSTALL=OFF \
    -DVTK_BUILD_ALL_MODULES=ON \
    -DVTK_GROUP_ENABLE_MPI=NO \
    -DVTK_GROUP_ENABLE_Web=NO \
    -DVTK_MODULE_ENABLE_VTK_vtkm=NO \
    -DVTK_USE_EXTERNAL=ON \
    -DVTK_MODULE_USE_EXTERNAL_VTK_exprtk=OFF \
    -DVTK_MODULE_USE_EXTERNAL_VTK_ioss=OFF \
    -DVTK_MODULE_USE_EXTERNAL_VTK_pegtl=OFF \
    -DVTK_MODULE_USE_EXTERNAL_VTK_verdict=OFF \
    -DVTK_WRAP_JAVA=OFF \
    -DVTK_WRAP_PYTHON=ON \
    -DVTK_SMP_IMPLEMENTATION_TYPE=TBB \
    -DVTK_SMP_ENABLE_SEQUENTIAL=ON \
    -DVTK_SMP_ENABLE_STDTHREAD=ON \
    -DVTK_SMP_ENABLE_OPENMP=ON \
    -DVTK_USE_VIDEO_FOR_WINDOWS=ON \
    -DVTK_ENABLE_OSPRAY=OFF \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
}
